package com.ideal.frame.util;

import cn.caict.tool.IdanchorTool;
import cn.caict.tool.SignXmlTool;
import cn.caict.tool.TarGzUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
public class XmlUtils {

    /**
     * 校验xml格式是否正确
     *
     * @param xsdFile
     * @param xmlFile
     * @return
     */
    public static boolean validateXMLSchema(File xsdFile, File xmlFile) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(xsdFile);
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(xmlFile));
        } catch (IOException | SAXException e) {
            log.error("xml文件格式校验异常，文件名：{}", xmlFile.getName(), e);
            return false;
        }
        return true;
    }

    /**
     * xml文件签名
     * 其中“秘钥字符串”由顶级审核机构信息并通过时，邮件发给客户，并由客户自行保存
     *
     * @param filePath      签名文件路径
     * @param privateKeyStr 签名秘钥
     */
    public static void signXml(String filePath, String privateKeyStr) {
        try {
            SignXmlTool.signXml(filePath, privateKeyStr);
        } catch (Exception e) {
            log.error("xml签名异常，文件路径：{}", filePath, e);
        }
    }

    /**
     * 保存xml
     *
     * @param document
     */
    public static void writeXml(Document document, String filePath, String fileName) {
        XmlUtil.toStr(document);
        if (!FileUtil.exist(filePath)) {
            FileUtil.mkdir(filePath);
        }
        FileOutputStream fileOutputStream = null;
        String path = filePath + fileName;
        try {
            fileOutputStream = new FileOutputStream(new File(path));
        } catch (FileNotFoundException e) {
            log.error("xml创建异常，文件路径：{}", filePath, e);
        }
        XmlUtil.write(document, fileOutputStream, "UTF-8", 2);
    }

    /**
     * 打包成tar.gz
     *
     * @param outPath  输出文件路径
     * @param filePath 需要打包的文件路径
     */
    public static void packetXml(String outPath, String fileName, List<String> filePath) {
        List<File> resourceList = new ArrayList<>();
        try {
            for (String s : filePath) {
                File file01 = new File(s);
                resourceList.add(file01);
            }
            if (!FileUtil.exist(outPath)) {
                FileUtil.mkdir(outPath);
            }
            TarGzUtil.packet(resourceList, outPath + fileName);
        } catch (Exception e) {
            log.error("xml打包异常，文件路径：{}，文件输出路径：{}", filePath.toString(), outPath, e);
        }
    }

    public void creatXmlDemo() {
        Document document = XmlUtil.createXml();
        Element root = document.createElement("rsmi:regSrvMainInfo");
        root.setAttribute("xmlns:rsmi", "urn:ietf:params:xml:ns:regsvrmaininfo-1.0");
        root.setAttribute("xmlns:bt", "urn:ietf:params:xml:ns:basetype-1.0");
        root.setAttribute("xmlns:rmci", "urn:ietf:params:xml:ns:regmancominfo-1.0");
        Element regManComInfo = document.createElement("rsmi:regManComInfo");
        Element regServerType = document.createElement("rmci:regServerType");
        Element servName = document.createElement("bt:servName");
        Element gnrlId = document.createElement("bt:gnrlId");
        servName.setTextContent("信通院");
        gnrlId.setTextContent("2631614400000001");
        regServerType.appendChild(servName);
        regServerType.appendChild(gnrlId);
        regManComInfo.appendChild(regServerType);
        root.appendChild(regManComInfo);
        document.appendChild(root);
        writeXml(document,"","");
    }

    public static void main(String[] args) throws FileNotFoundException {
        String privateKeyStr = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCnBs196+qSJZ1r+Wu13WGivGP3JdgUY0T/bTVdZpAN5lcSQb8IPcrte2Jt1mow2xAWHEygj+ZTFNk1oG+q+ftY0YMzXSQTEMTS5T0dVTPfIwLm2Q0sM1sjTK2liIk9VJSGMlaEco6UpjhNnZn29K52Pd0QfezW/PKQ/TtuSIje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YjakrIvOGomwcRLaKUem2kP2xFaP304E7ss2BfMOs/dYJ9Dj54+j8lgsoCk8Z68kxLCI0V6apEU/ZjFGBPV7ELJeZ2Hv96cxyJWI4yoxZaBJ51kU2SJ05z3MrPIWIIbhNIJn4pAwDnd/8Vas7bix+3qb0sGlqQKBgA58kRVmFIB/eaR7DlX5bum7I/QdrsNi91tGVMeg6ralNEXuWB+6JJ5IQ/MeNx927P4CLhM6ccIPAsdMkMc7g+TyuasCHLTj32RCJdDJlfBS1/VLZFDMZH0/bR5lreE3ib7XnHArrLH5y+/YSn/RAjd/ZEw9Ln2ZsFMnhWI/EU5Z";
        String path = ResourceUtils.getURL("classpath:").getPath();
        String localPath = path + "static/xml/WDCSJCKJ76681932015373867.xml";
        XmlUtils.signXml(localPath, privateKeyStr);
        String outPath = path + "static/tar/WDCSJCKJ76681932015373867.tar.gz";
        List<String> filePath = Arrays.asList(localPath, localPath);
//        XmlUtils.packetXml(outPath, filePath);
    }

    private void t() {
        String gnrlId = "2021071515380005268896";
//        String gnrlId = "2021062917050001046465";
        String time = DateUtil.format(new Date(System.currentTimeMillis()), "yyyyMMddHHmmss");
        String privateKeyStr = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCCWA3fdZl8thfu/uzgRsPsqoqPUs4853PeeIOdkYYR5DKGlof1S7b8m0HGE07vAZmZhl8yPpadQGqPnyulmKKGaaNcQ3/VkJzln3iErsIX1cY8uE6iyb1jjoFKRjrpmp7j0Zt/sFRzdTHx7CGTxUwXuYbc6yHgwza70XMVop2EHg6W9pxrbcoTGPxmFn1oT4xAxxc4ooOKi60kvXhXTrEAz/yXE5bmNrFjqSXWT9GZ1B2DdZvS/7J8H1xWQ+iJfFBoqHOi7dXXcxBFSefk3gVhOcxzJGeP9AwVx0vN4HDJR5T2xWOaV1tvgPpnARtiS45JC/NwsCE+XV9HDZk2F9YZAgMBAAECggEAboNWD9bXskN5+2stT+LwD2NxY5+juUPMnB22CgzujZDb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EiWOm+W+3cpZiY2uFhjJMUCy3bh6kpp1JSELMfpQlKfTaYJvH8Bgq2xzvzwN2wqaLpvQBPLVXQJsupodgtw+SJ2URmiUpRk0/vVD4qmb7+bNiJxw3DfAk8O/OzD+U2i9C12du6DJVMB478kCgYEA6mO7EzHWKYdg6VICY/hm2o9uGfOZxujXPsgHg0fQGL8tt3G5W4uq6uBDSzGZUCuxhulyc+ZDhlAne9QXnBtlsnTZe2Qq+hhq2imjSE8wKhKfJHPzb5ribUgDXMlxY01pZyhu/h6rM62pUHmA0NNkurU8J7Q5DOiTdgIabk9rGM0CgYA0z2P93fjiiawXYjakrIvOGomwcRLaKUem2kP2xFaP304E7ss2BfMOs/dYJ9Dj54+j8lgsoCk8Z68kxLCI0V6apEU/ZjFGBPV7ELJeZ2Hv96cxyJWI4yoxZaBJ51kU2SJ05z3MrPIWIIbhNIJn4pAwDnd/8Vas7bix+3qb0sGlqQKBgA58kRVmFIB/eaR7DlX5bum7I/QdrsNi91tGVMeg6ralNEXuWB+6JJ5IQ/MeNx927P4CLhM6ccIPAsdMkMc7g+TyuasCHLTj32RCJdDJlfBS1/VLZFDMZH0/bR5lreE3ib7XnHArrLH5y+/YSn/RAjd/ZEw9Ln2ZsFMnhWI/EU5Z";
        String timeAndGnrlId = time + gnrlId;
        String authInfo = IdanchorTool.getAuthInfo(privateKeyStr, timeAndGnrlId);
        System.out.println(authInfo);
        System.out.println("time:" + time);
        System.out.println("timeAndGnrlId:" + timeAndGnrlId);
    }
}
